package com.cyrus.vodtest;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/12/10
 */
public class testDemo {
    public static void main(String[] args) throws Exception {
        String accessKeyId = "LTAI4Fy48qb4brGj88Q3kqGZ";
        String accessKeySecret = "7RCJNUceY26WhiP3nkF3KFpRhPtBCQ";
        String title = "6 - What If I Want to Move Faster";
        String fileName = "D:/6 - What If I Want to Move Faster.mp4";
        UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）*/
        request.setTaskNum(1);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);
        if (response.isSuccess()) {
            //获取上传后的id
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
            /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }

    }

    public static void getAuth() throws Exception {
        //初始化对象
        DefaultAcsClient client = InitObject.initVodClient("LTAI4Fy48qb4brGj88Q3kqGZ", "7RCJNUceY26WhiP3nkF3KFpRhPtBCQ");
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        request.setVideoId("a9617fa45c7a4c47a5678a382082293b");

        try {
            response = client.getAcsResponse(request);
            //播放凭证
            System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
            //VideoMeta信息
            System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }

    //根据视频id获取视频播放地址
    public static void getPlayUrl() throws Exception {
        //a9617fa45c7a4c47a5678a382082293b
        //初始化对象
        DefaultAcsClient client = InitObject.initVodClient("LTAI4Fy48qb4brGj88Q3kqGZ", "7RCJNUceY26WhiP3nkF3KFpRhPtBCQ");

        //创建获取视频地址request和response
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();

        //向request对象里面设置视频id
        request.setVideoId("a9617fa45c7a4c47a5678a382082293b");

        //调用初始化对象里面的方法
        response = client.getAcsResponse(request);

        try {
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
            }
            //Base信息
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
    }


    @Test
    public void test2() throws Exception{
        Socket socket = new Socket("127.0.0.1", 8888);
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        String message = "hi,server";
        os.write(message.getBytes(), 0, message.length());
        os.close();
        is.close();
    }

    @Test
    public void test1() throws Exception{
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket accept = serverSocket.accept();
        OutputStream os = accept.getOutputStream();
        InputStream is = accept.getInputStream();

//        byte[] bytes = new byte[1024];
//        int len = 0;
//        if ((len = is.read(bytes)) != -1) {
//            System.out.println(new String(bytes,0,len));
//        }

        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String info = br.readLine();
        System.out.println(info);


    }

    @Test
    public void test20() throws Exception{
        FileReader fileReader = new FileReader("a.txt");
        FileWriter fileWriter = new FileWriter("b.txt");
        char[] chars = new char[1024];
        int len = 0;
        if ((len = fileReader.read(chars)) != -1) {
            fileWriter.write(chars,0,len);
        }
    }

    @Test
    public void test21() throws Exception{
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("a", "2");
        hashMap.put("b", "2");
        Set<String> keyset = (Set)hashMap.keySet();
        System.out.println(keyset);
        Iterator<String> iterator = keyset.iterator();
        if (iterator.hasNext()) {
            String next = iterator.next();
            hashMap.get(next);
        }

    }


}


